<?php
class Sosweb_Util_Image {
	public static function getResizedSize($img, $maxWidth = -1, $maxHeight = -1) {
		try {
			$imageOrgSize = getimagesize("http://".$_SERVER['HTTP_HOST'].str_replace(" ", "%20", $img));
//			$imageOrgSize = getimagesize("http://alfa.pl/public/files/promos/a1.png");

			if ($imageOrgSize != null) {
				$resizeFactor = 1;

				if ($imageOrgSize[0] > $imageOrgSize[1]) {
					if ($maxWidth > 0) {
						$resizeFactor = $maxWidth/$imageOrgSize[0];
					}
					else if ($maxHeight > 0) {
						$resizeFactor = $maxHeight/$imageOrgSize[1];
					}
				}
				else {
					if ($maxHeight > 0) {
						$resizeFactor = $maxHeight/$imageOrgSize[1];
					}
					else if ($maxWidth > 0) {
						$resizeFactor = $maxWidth/$imageOrgSize[0];
					}
				}

				$resizedImageSize = array('width'=>$imageOrgSize[0]*$resizeFactor, 'height'=>$imageOrgSize[1]*$resizeFactor);

				return $resizedImageSize;
			}
			else {
				return null;
			}
		}
		catch (Exception $ex) {
			return null;
		}
	}

	public static function getIconForFileType($filename) {
		$availableExtensions = array(
			"pdf"=>"pdf",
			"zip"=>"rar", "rar"=>"rar", "7z"=>"rar", "gz"=>"rar",
			"exe"=>"exe",
			"log"=>"log",
			"conf"=>"txt", "txt"=>"txt",
			"xls"=>"xls", "xlsx"=>"xls",
			"doc"=>"doc",
			"avi"=>"avi", "mpeg"=>"avi", "mpg"=>"avi", "wmv"=>"avi",
			"doc"=>"doc", "docx"=>"doc", "rtf"=>"doc",
			"php"=>"php",
			"java"=>"java",
			"mp3"=>"mp3", "wav"=>"mp3",
			"html"=>"html", "htm"=>"html",
			"css"=>"css");

		if (!Sosweb_Util_File::isImage($filename)) {
			$ext = Sosweb_Util_String::get_file_extension($filename);
			$extImage = (isset($availableExtensions[$ext]) ? $availableExtensions[$ext] : null);

			if ($extImage != null) {
				return "/img/file_".$extImage.".png";
			}
			else {
				return "/img/file.png";
			}
		}
		else {
			return null;
		}
	}
}